package com.lsx.controller;

import com.lsx.pojo.User;
import com.lsx.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by pc on 2017/11/18.
 */
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserServiceImpl userservice;

    @RequestMapping("login")
    @ResponseBody
    public int login(String uname, String pwd,HttpServletRequest request) {
        User user = userservice.login(uname, pwd);
        if (user == null) {
            return 0;
        } else {
            HttpSession session = request.getSession();
            session.setAttribute("user", user);
            return 1;
        }
    }

    @RequestMapping("register")
    @ResponseBody
    public int register(String uname, String pwd) {
        User user = new User();
        user.setUsername(uname);
        user.setPassword(pwd);
        int cos = userservice.register(user);
        return cos;
    }

}
